package xjtu.se.articlecommunity.user.articledetail.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import xjtu.se.articlecommunity.user.articledetail.dto.*;

import java.util.ArrayList;
import java.util.List;


public interface ArticleDetailDao {
    @Select("select t_article.a_title,t_article.a_html,t_article.a_label from t_article,t_user where t_article.u_id=t_user.u_id and t_user.u_name=#{u_name}")
    Article getInfo(String u_name);
    @Update("update t_article_like set ifchecked=#{likingState} where u_id=#{u_id} and a_id=#{a_id}")
    void updateLikeState(String u_id, String a_id, int likingState);

    @Select("select count(*) from t_comment where c_id=#{c_id}")
    int getC_idNum(String c_id);
    @Insert("insert into t_comment (c_id,u_id,a_id,c_detail,if_fc_id) values(#{id},#{u_id},#{a_id},#{commentcontent},0)")
    void insertComment(String commentcontent, String u_id, String a_id, String id);
    @Select("select t_user.u_name,t_user.u_id from t_user,t_article_like where t_user.u_id=t_article_like.u_id and a_id=#{a_id} and t_article_like.ifchecked=1")
    List<User> getLikingUsers(String a_id);

    @Select("select count(*) from t_article_like where a_id=#{a_id}")
    int getLikeNum(String a_id);
    @Select("select count(*) from t_comment where a_id=#{a_id};")
    int getCommentNum(String a_id);
    @Select("select art.a_date,art.a_title,art.u_id,users.u_name,users.u_avatar,art.a_html from t_article art,t_user users\n" +
            "where art.u_id=users.u_id and art.a_id=#{a_id};")
    Article getArticle(String a_id);
    @Select("select t_user.u_name,t_comment.u_id,t_user.u_avatar,t_comment.c_date,t_comment.c_detail,t_comment.c_id,t_comment.grandfc_id,t_comment.fc_id from t_comment,t_user where t_comment.u_id=t_user.u_id and t_comment.a_id=#{a_id} and t_comment.if_fc_id=#{if_fc_id} order by t_comment.c_date ")
    ArrayList<CommentInfo> getCommnetInfo(String a_id,int if_fc_id);
    @Select("select t_user_tab.ut_name from t_user,t_user_tab,t_article,user_tab where t_user.u_id=user_tab.u_id and user_tab.ut_id=t_user_tab.ut_id and t_article.u_id=t_user.u_id and a_id=#{a_id}  ")
    List<String> getUserTab(String a_id);
    @Select("select at_name from t_article_tab tab,art_tab arttab,t_article art where art.a_id=arttab.a_id and arttab.at_tab=tab.at_tab and art.a_id=#{a_id};")
    String[] getArticleTabs(String a_id);
    @Select("select users.u_name from t_comment com,t_user users where com.u_id=users.u_id and com.c_id=#{fc_id}")
    String getBu_name(String fc_id);
    @Select("select t_user.u_name,t_comment.c_id,t_comment.grandfc_id,t_comment.fc_id,t_comment.a_id,t_comment.if_fc_id from t_comment,t_user where t_comment.u_id=t_user.u_id and t_comment.c_id=#{c_id}")
    ParentComment getParentCommentByC_id(String c_id);
    @Select("select count(*) from t_comment where t_comment.c_id=#{newC_id}")
    int ifExist(String newC_id);
    @Insert("insert into t_comment (c_id,u_id,a_id,c_detail,fc_id,grandfc_id,if_fc_id) values(#{newC_id},#{u_id},#{a_id},#{reply_detail},#{c_id},#{grandfc_id},1)")
    void insertReply(String newC_id, String c_id, String a_id, String u_id, String reply_detail, String grandfc_id);
    @Insert("insert into t_article_like (u_id,a_id,ifchecked) values (#{u_id},#{a_id},#{i})")
    void insertRecord(String u_id, String a_id, int i);
    @Update("update t_article_like set ifchecked=#{ifchecked}  where u_id=#{u_id} and a_id=#{a_id}")
    void updateRecord(String u_id, String a_id, int ifchecked);
    @Select("select count(*) from t_article_like lik where lik.a_id=#{a_id} and lik.u_id=#{u_id}")
    int getLikingState(String u_id, String a_id);
    @Select("select ifchecked from t_article_like lik where lik.a_id=#{a_id} and lik.u_id=#{u_id}")
    int getIfChecked(String u_id, String a_id);
    @Select("select count(*) from t_comment_like lik where lik.u_id=#{u_id} and lik.c_id=#{c_id}")
    int getReplyLikingState(String u_id, String c_id);
    @Select("select ifchecked from t_comment_like lik where lik.u_id=#{u_id} and lik.c_id=#{c_id}")
    int getReplyIfChecked(String u_id, String c_id);
    @Update("update t_comment_like set ifchecked=#{ifchecked}  where u_id=#{u_id} and c_id=#{c_id}")
    void updateReplyRecord(String u_id, String c_id, int ifchecked);
    @Insert("insert into t_comment_like (u_id,c_id,ifchecked) values (#{u_id},#{c_id},#{i})")
    void insertReplyRecord(String u_id, String c_id, int i);
    @Select("select count(*) from t_comment_like where t_comment_like.c_id=#{c_id} and t_comment_like.ifchecked=1")
    int getCommentLikeNum(String c_id);
    @Select("select count(*) from t_subscription subs where subs.a_id=#{a_id} and ifcheckd=1")
    int getCollectNumByA_id(String a_id);

    @Select("select count(*) from t_follow lik where lik.u_id=#{u_id} and lik.befollowed_u_id=#{ba_id}")
    int getAttendState(String u_id, String ba_id);
    @Select("select ifchecked from t_follow lik where lik.u_id=#{u_id} and lik.befollowed_u_id=#{ba_id}")
    int getAttendIfChecked(String u_id, String ba_id);
    @Update("update t_follow set ifchecked=#{ifchecked}  where u_id=#{u_id} and befollowed_u_id=#{ba_id}")
    void updateAttendRecord(String u_id, String ba_id, int ifchecked);
    @Insert("insert into t_follow(u_id,befollowed_u_id,ifchecked) values (#{u_id},#{ba_id},#{i})")
    void insertAttendRecord(String u_id, String ba_id, int i);

    @Select("select count(*) from t_subscription lik where lik.u_id=#{u_id} and lik.a_id=#{a_id}")
    int getCollectState(String u_id, String a_id);
    @Select("select ifcheckd from t_subscription lik where lik.u_id=#{u_id} and lik.a_id=#{a_id}")
    int getCollectIfChecked(String u_id, String a_id);
    @Update("update t_subscription  set ifcheckd=#{ifchecked}  where u_id=#{u_id} and a_id=#{a_id}")
    void updateCollectRecord(String u_id, String a_id, int ifchecked);
    @Insert("insert into t_subscription (u_id,a_id,ifcheckd) values (#{u_id},#{a_id},#{i})")
    void insertCollectRecord(String u_id, String a_id, int i);

    @Select("select u_id from t_article art where art.a_id=#{a_id}")
    String getU_idByA_id(String a_id);
    @Select("select count(*) from t_comment_like where u_id=#{u_id} and c_id=#{c_id} and ifchecked=1")
    int ifCommentOrRely(String u_id, String c_id);
}
